package com.haha.gulimall.product.web;

import com.haha.gulimall.product.entity.CategoryEntity;
import com.haha.gulimall.product.service.impl.CategoryServiceImpl;
import com.haha.gulimall.product.vo.Category2VO;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
public class indexController {
    @Resource
    CategoryServiceImpl categoryService;
    @Resource
    RedissonClient redisson;


    @GetMapping({"/","/index.html"})
    public String indexPage(Model model) {
        List<CategoryEntity> categoryEntities = categoryService.getCategroyLevel1();
        model.addAttribute("categorys",categoryEntities);
        return "index";
    }

    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String ,List<Category2VO>> getCategoryList(){
        Map<String ,List<Category2VO>> res = categoryService.getCategroyJson();
        return res;
    }

    @ResponseBody
    @GetMapping("/hello")
    public String gethello(){
        RLock haha = redisson.getLock("haha");
        haha.lock();
        try {
            System.out.println("枷锁成功，执行业务"+Thread.currentThread().getId());
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            System.out.println("释放锁成功"+Thread.currentThread().getId());
            haha.unlock();
        }
   return "hello";

    }
}
